//
//  MTWelcomeViewController.swift
//  MTWeiBo
//
//  Created by user on 2017/7/25.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
//欢迎界面
class MTWelcomeViewController: UIViewController {

    //懒加载控件
    fileprivate lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    fileprivate lazy var userImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        if let imageUrl = MTUserAccountViewModel.shardViewModel.userAccount?.avatar_large {
            let url = URL(string: imageUrl)
            imageView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        //切圆
        imageView.layer.cornerRadius = 45
        //裁剪
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    //消息内容
    fileprivate lazy var messageLabel: UILabel = {
       let label = UILabel()
        if let name = MTUserAccountViewModel.shardViewModel.userAccount?.name {
            label.text = "欢迎回来\(name)"
        }else{
            label.text = "欢迎回来"
        }
        
        //属相
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.darkGray
        label.alpha = 0
        return label
    }()
    
    
    //自定义视图
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }

    //视图已经显示完成
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        startSpringAnimation()
    }
    
    private func setupUI() {
        //添加视图
        view.addSubview(userImageView)
        view.addSubview(messageLabel)
        //约束
        userImageView.snp.makeConstraints { (make) in
            make.top.equalTo(view).offset(200)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView.snp.bottom).offset(10)
            make.centerX.equalTo(view)
        }
        
    }
    
    //开启弹簧动画
    private func startSpringAnimation() {
        //更新约束
        userImageView.snp.makeConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        
        //弹簧效果
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: { 
            self.view.layoutIfNeeded()
        }, completion: { (_) in
            
            //文字渐变
            UIView.animate(withDuration: 1, animations: { 
                self.messageLabel.alpha = 1
            }, completion: { (_) in
                
                //切换更是图控制器
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: SwitchRootVCNotificationName), object: nil)
                
            })
        })
    }
  

}
